#import "FriendsView.h"
#import "FriendsSourceListElement.h"
#import "Peers.h"
#import "SolarSeekApplication.h"
#import "SolarSeekEngine.h"
#import "ChatsController.h"
#import "ChatElements.h"
#import "BrowseElement.h"

@implementation FriendsView

- (id)initWithFrame:(NSRect)frameRect {
	if ((self = [super initWithFrame:frameRect]) != nil) {
		_element = [[FriendsSourceListElement alloc] initWithView:self];
	}
	return self;
}

- (void)dealloc {
	[_element release];
	[super dealloc];
}

- (void)awakeFromNib {
	[nibTableFriends setTarget:self];
	[nibTableFriends setDoubleAction:@selector(startPrivateChats:)];
}

- (void)selectChat:(Chat *)chat {
	[NSApp expandSourceListElement:[ChatsElement elementChatsRoot]];
	[NSApp selectSourceListElement:[ChatsElement elementForChat:chat]];
}

- (IBAction)startPrivateChats:(id)sender {
	NSEnumerator *e = [[nibControllerParticipants selectedObjects] objectEnumerator];
	Peer *p;
	Chat *chat=nil;
	while(p = [e nextObject])
		chat = [[[NSApp engine] chatsController] chatWithPeer:p];
		
	if(chat) {
		[self performSelector:@selector(selectChat:) withObject:chat afterDelay:0.02];
	}

}

- (FriendsSourceListElement *)element { return _element; }

- (void)addDidEnd:(NSWindow *)sheet returnCode:(int)code contextInfo:(void *)info {
	[sheet orderOut:self];
	NS_DURING
		if(code==NSOKButton) {
			NSString *name = [nibTextFriend stringValue];
			Peer *peer = [[NSApp engine] peerNamed:name];
			[_element addFriend:peer];
		}
	NS_HANDLER
		NSLog(@"Encountered an exception: %@",localException);
	NS_ENDHANDLER
}


- (IBAction)add:(id)sender
{
	[NSApp beginSheet:nibPanelAdd modalForWindow:[NSApp keyWindow] modalDelegate:self didEndSelector:@selector(addDidEnd:returnCode:contextInfo:) contextInfo:nil];
}

- (NSArray *)participantsSelected { 
	NSView *v = [[NSApp selectedSourceListElement] view];
	if(v!=self && [v respondsToSelector:@selector(participantsSelected)])
		return [v performSelector:@selector(participantsSelected)];
	else
		return [nibControllerParticipants selectedObjects]; 
}

- (void)copyString:(NSString *)string {
	NSPasteboard *pb = [NSPasteboard generalPasteboard];
	[pb addTypes:[NSArray arrayWithObjects:NSStringPboardType,nil] owner:self];
	[pb setString:string forType:NSStringPboardType];
}

- (IBAction)copyPeerNames:(id)sender {
	NSArray *a = [self participantsSelected];
	NSEnumerator *e = [a objectEnumerator];
	NSMutableString *string = [NSMutableString string];
	Peer *p;
	BOOL start = YES;
	while(p = [e nextObject]) {
		if(start)
			start = NO;
		else
			[string appendString:@", "];
		[string appendString:[p name]];
	}
	[self copyString:string];
}

- (IBAction)addSelected:(id)sender
{
	NSEnumerator *e = [[self participantsSelected] objectEnumerator];
	Peer *p;
	while(p = [e nextObject]) {
		[_element addFriend:p];
	}
}

- (void)addFriendsSelected:(id)sender {
	[self addSelected:sender];
}

- (IBAction)no:(id)sender
{
	[NSApp endSheet:nibPanelAdd returnCode:NSCancelButton];
}

- (IBAction)removeSelected:(id)sender
{
	NSEnumerator *e = [[nibControllerParticipants selectedObjects] objectEnumerator];
	Peer *p;
	while(p = [e nextObject]) {
		[_element removeFriend:p];
	}
}

- (IBAction)yes:(id)sender
{
	[NSApp endSheet:nibPanelAdd returnCode:NSOKButton];
}

@end
